--
-- Author: shunguo.chen
-- Date: 2018-09-04 20:51:37
--
-- TeamMatchMemberPanel 组队查找器里面我的队伍信息

local NewTeamMemberPanel = import(".NewTeamMemberPanel")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node)
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init()
    return sprite
end

function ClassRef.create(node)
	return ClassRef.createWithData(nil, node)
end

function ClassRef:init(data)
    self.super.init(self, data)

    self._memberPanel = NewTeamMemberPanel.create(self._ui.player)
    self._memberPanel:registerOnOperate(function(op, ...)
            executeFunc(self._onOperate, op, ...) -- 交由controller处理
        end)

    local panel = self._ui.Panel_data

    self._applyBtn = panel:getChildByName("Button_apply")
    self._applyNewIcon = self._applyBtn:getChildByName("dotNew")
    self._applyBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "openApply")
        self._applyNewIcon:setVisible(false)
    end)

    self._setBtn = panel:getChildByName("Button_set")
    self._setBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "openSet")
    end)

    self._leaveBtn = panel:getChildByName("Button_leave")
    self._leaveBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "leaveTeam")
    end)
    self._legionBtn = panel:getChildByName("Button_legion")
    self._legionBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "changeGrade")
    end)
    self._teamBtn = panel:getChildByName("Button_team")
    self._teamBtn:addClickEventListener(function()
        executeFunc(self._onOperate, "changeGrade")
    end)

    self._numPanel = panel:getChildByName("Panel_num")
    self._combatText = panel:findChild("Panel_num/combat_num")
end

function ClassRef:refreshApplyNew(b)
    self._applyNewIcon:setVisible(b)
end

-- target = TeamTarget
function ClassRef:refreshTarget(target)
    self._target = target

end

-- data = TeamVo
function ClassRef:refresh()
    local team = self._data
    local target = self._target
    local isInCrossMatch

    self._memberPanel:setData(team)
    local isMyTeam = team:isMyTeam()
    local isLegion = team:isLegion()
    self._applyBtn:setVisible(isMyTeam)
    self._legionBtn:setVisible(TEAM_LEGION_ENABLE and isMyTeam and not isLegion)
    self._teamBtn:setVisible(TEAM_LEGION_ENABLE and isMyTeam and isLegion)

    local maxNum = target:getMaxNum()
    local isWithPartner = target:isWithPartner()
    -- 刷新超限制人数禁止标志
    self._memberPanel:refreshForbiddenIconStatus(maxNum)
    -- 刷新队长随从信息
    if isWithPartner then
        self._memberPanel:refreshPartnerInfo(maxNum, team:getLeaderPartnerInfo())
    end

    -- 显示战斗力
    local isBalance = self._target:isBalanceNumType()
    self._numPanel:setVisible(not isBalance)
    if not isBalance then
        local needCombat = target:getCombat()
        local teamCombat = team:getTeamTotalCapacity(true)
        self._combatText:setString(teamCombat)

        local color = teamCombat < needCombat and display.COLOR_RED or display.COLOR_WHITE
        self._combatText:setTextColor(color)
    end
end

return ClassRef

